package com.yimaxiaoerlang.module_signalling;

import android.util.Log;

import com.yimaxiaoerlang.module_signalling.core.YMMessageType;
import com.yimaxiaoerlang.module_signalling.core.YMRTMChannelLeaveListener;
import com.yimaxiaoerlang.module_signalling.core.YMRTMChannelListener;
import com.yimaxiaoerlang.module_signalling.core.YMRTMJoinChannelCallback;
import com.yimaxiaoerlang.module_signalling.core.YMRTMJoinChannelStatusCode;
import com.yimaxiaoerlang.module_signalling.core.YMRTMLeaveChannelCallback;
import com.yimaxiaoerlang.module_signalling.core.YMRTMLeaveChannelStatusCode;
import com.yimaxiaoerlang.module_signalling.core.YMRTMSendChannelCallback;
import com.yimaxiaoerlang.module_signalling.core.YMRTMSendChannelMessageStatusCode;
import com.yimaxiaoerlang.module_signalling.core.YMSocketManager;
import com.yimaxiaoerlang.module_signalling.model.YMRTMMessage;
import com.yimaxiaoerlang.module_signalling.utils.MessageUtils;

import java.util.ArrayList;

public class YMRTMChannel {
    private static final String TAG = YMRTMChannel.class.getSimpleName();
    private YMSocketManager mClient;
    private String channelId;
    private String uid;
    private YMRTMChannelLeaveListener leave;
    private ArrayList<String> members = new ArrayList<>();
    private YMRTMChannelListener channelListener;

    public YMRTMChannel(YMSocketManager mClient, String channelId, String uid, YMRTMChannelLeaveListener leave) {
        this.mClient = mClient;
        this.channelId = channelId;
        this.uid = uid;
        this.leave = leave;
    }


    /**
     * 加入房间
     */
    public void joinChannel(YMRTMJoinChannelCallback callback) {
        String msg = MessageUtils.createJoinMessage(uid, channelId, "加入房间");
        mClient.sendMessage(msg);
        if (callback != null) {
            callback.completion(YMRTMJoinChannelStatusCode.Success);
        }
    }

    /**
     * 离开房间
     */
    public void leaveChannel(YMRTMLeaveChannelCallback callback) {
        String msg = MessageUtils.createLeaveMessage(uid, channelId, "离开房间");
        mClient.sendMessage(msg);
        if (leave != null) {
            leave.onLeave();
        }
       if (callback!=null){
           callback.completion(YMRTMLeaveChannelStatusCode.Success);
       }
    }

    public void onMessage(YMRTMMessage msg) {
        Log.e(TAG, "onMessage: TYPE -> " + msg.getMsgType() + ", UID -> " + msg.getUid() + ", CUR_UID -> " + uid  + ", CONTENT: " + msg.getData());
        if (msg.getUid().equals(uid)) {
            Log.e(TAG, "onMessage: UID相同，return "  + ", CONTENT: " + msg.getData());
            return;
        }
        Log.e(TAG, "onMessage: UID不一样 UID: " + msg.getUid() + ", TYPE: " + msg.getMsgType() + ", CONTENT: " + msg.getData());
        switch (msg.getMsgType()) {
            case YMMessageType.RTMMessageTypeJoinChannel:
                if (channelListener != null) {
                    channelListener.onMemberJoined(msg.getUid());
                }
                break;
            case YMMessageType.RTMMessageTypeLeaveChannel:
                if (channelListener != null) {
                    channelListener.onMemberLeaved(msg.getUid());
                }
                break;
            case YMMessageType.RTMMessageTypeChannelMembers:
                String[] array = msg.getData().split(",");
                members.clear();
                for (String s : array) {
                    members.add(s);
                }
                break;
            default:
                if (channelListener != null) {
                    channelListener.onReceiveMessage(msg.getUid(), msg.getData());
                }
                break;
        }
    }

    public void sendMessage(String message, YMRTMSendChannelCallback callback) {
        String msg = MessageUtils.createChannelMessage(uid, channelId, message);
        mClient.sendMessage(msg);
        if (callback != null) {
            callback.completion(YMRTMSendChannelMessageStatusCode.Success);
        }
    }

    public void sendVoIPMessage(String message, YMRTMSendChannelCallback callback) {
        String msg = MessageUtils.createChannelVoIPMessage(uid, channelId, message);
        mClient.sendMessage(msg);
        if (callback != null) {
            callback.completion(YMRTMSendChannelMessageStatusCode.Success);
        }
    }

    public String getChannelId() {
        return channelId;
    }

    public void setChannelListener(YMRTMChannelListener channelListener) {
        this.channelListener = channelListener;
    }
}
